<template>
  <view class="food-recognition">    
    <!-- 识别表单 -->
    <view class="recognition-form">
      <!-- 运动模式 -->
      <view class="form-item required">
        <view class="form-label">运动模式</view>
        <view class="form-input">
          <input type="text" placeholder="请输入食物名称" />
        </view>
      </view>
      
      <!-- 错误提示 -->
      <view class="error-tip">
        <text class="error-icon">!</text>
        <text class="error-text">请填写食物名称(必填项)</text>
      </view>
      
      <!-- 食物图片 -->
      <view class="form-item">
        <view class="form-label">食物图片</view>
        <view class="image-uploader">
          <view class="upload-placeholder">
            <text class="plus-icon">+</text>
          </view>
        </view>
      </view>
      
      <!-- 上传提示 -->
      <view class="upload-tip">
        <text class="tip-text">仅限于食物识别，其他图片无效</text>
        <text class="tip-text">请勿多次上传无效图片，否则系统将会禁用该功能</text>
      </view>
      
      <!-- 营养价值 -->
      <view class="nutrition-section">
        <view class="nutrition-title">营养价值</view>
        <view class="nutrition-subtitle">每100克重量食物</view>
      </view>
      
      <!-- 智能分析按钮 -->
      <view class="analyze-btn">
        <text class="btn-icon">🤖</text>
        <text class="btn-text">开始智能分析</text>
      </view>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      // 表单数据
      formData: {
        foodName: '',
        foodImage: null
      },
      // 错误信息
      errorMsg: '',
      // 是否显示错误提示
      showError: true,
      // 分析结果
      analysisResult: null
    };
  },
  methods: {
    // 返回上一页
    goBack() {
      uni.navigateBack();
    },
    
    // 选择图片
    chooseImage() {
      uni.chooseImage({
        count: 1,
        sizeType: ['compressed'],
        sourceType: ['album', 'camera'],
        success: (res) => {
          this.formData.foodImage = res.tempFilePaths[0];
        }
      });
    },
    
    // 开始分析
    startAnalysis() {
      // 表单验证
      if (!this.formData.foodName) {
        this.errorMsg = '请填写食物名称(必填项)';
        this.showError = true;
        return;
      }
      
      if (!this.formData.foodImage) {
        uni.showToast({
          title: '请上传食物图片',
          icon: 'none'
        });
        return;
      }
      
      // 显示加载中
      uni.showLoading({
        title: '分析中...'
      });
      
      // 模拟分析过程
      setTimeout(() => {
        uni.hideLoading();
        
        // 模拟分析结果
        this.analysisResult = {
          name: this.formData.foodName,
          calories: 120,
          nutrition: {
            carbs: 22.9,
            protein: 22.9,
            fat: 22.9
          }
        };
        
        // 跳转到食物详情页
        uni.navigateTo({
          url: '/pages/food-detail/index'
        });
      }, 2000);
    }
  }
}
</script>

<style lang="scss">
.food-recognition {
  min-height: 100vh;
  background-color: #f5f5f5;
  
  // 导航栏
  .nav-bar {
    display: flex;
    align-items: center;
    height: 88rpx;
    padding: 0 30rpx;
    background-color: #fff;
    
    .back-icon {
      width: 44rpx;
      height: 44rpx;
      display: flex;
      align-items: center;
      justify-content: center;
    }
    
    .title {
      flex: 1;
      text-align: center;
      font-size: 36rpx;
      font-weight: 500;
    }
    
    .action-icons {
      display: flex;
      gap: 30rpx;
      
      .iconfont {
        font-size: 44rpx;
      }
    }
  }
  
  // 识别表单
  .recognition-form {
    padding: 30rpx;
    
    // 表单项
    .form-item {
      margin-bottom: 30rpx;
      
      &.required {
        .form-label::before {
          content: '*';
          color: #ff3b30;
          margin-right: 8rpx;
        }
      }
      
      .form-label {
        font-size: 28rpx;
        color: #333;
        margin-bottom: 20rpx;
      }
      
      .form-input {
        height: 90rpx;
        background-color: #fff;
        border-radius: 8rpx;
        padding: 0 20rpx;
        
        input {
          width: 100%;
          height: 100%;
          font-size: 28rpx;
        }
      }
    }
    
    // 错误提示
    .error-tip {
      display: flex;
      align-items: center;
      margin-top: -20rpx;
      margin-bottom: 30rpx;
      
      .error-icon {
        width: 30rpx;
        height: 30rpx;
        border-radius: 50%;
        background-color: #ff3b30;
        color: #fff;
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 24rpx;
        margin-right: 10rpx;
      }
      
      .error-text {
        font-size: 24rpx;
        color: #ff3b30;
      }
    }
    
    // 图片上传
    .image-uploader {
      .upload-placeholder {
        width: 100%;
        height: 300rpx;
        background-color: #fff;
        border: 2rpx dashed #ddd;
        border-radius: 8rpx;
        display: flex;
        align-items: center;
        justify-content: center;
        
        .plus-icon {
          font-size: 80rpx;
          color: #999;
        }
      }
    }
    
    // 上传提示
    .upload-tip {
      margin-top: -20rpx;
      margin-bottom: 30rpx;
      
      .tip-text {
        display: block;
        font-size: 24rpx;
        color: #999;
        line-height: 1.5;
        
        &::before {
          content: '注：';
          color: #ff3b30;
        }
      }
    }
    
    // 营养价值部分
    .nutrition-section {
      margin-top: 40rpx;
      margin-bottom: 20rpx;
      
      .nutrition-title {
        font-size: 32rpx;
        font-weight: 500;
        color: #333;
        margin-bottom: 10rpx;
      }
      
      .nutrition-subtitle {
        font-size: 26rpx;
        color: #666;
      }
    }
    
    // 分析按钮
    .analyze-btn {
      width: 100%;
      height: 90rpx;
      background-color: #e1f5fe;
      border-radius: 45rpx;
      display: flex;
      align-items: center;
      justify-content: center;
      margin-top: 40rpx;
      
      .btn-icon {
        font-size: 36rpx;
        margin-right: 10rpx;
      }
      
      .btn-text {
        font-size: 30rpx;
        color: #0288d1;
      }
    }
  }
}
</style>
